package com.pengpeng.flash.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pengpeng.flash.common.R;
import com.pengpeng.flash.entity.User;
import com.pengpeng.flash.service.UserService;
import com.pengpeng.flash.utils.SMSUtils;
import com.pengpeng.flash.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 发送手机验证码
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsgApi(@RequestBody User user, HttpServletRequest request){
        //获取手机号
        String phone = user.getPhone();
        if (StringUtils.isNotEmpty(phone)){
            //生成随机验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("验证码为:{}",code);
            //调用阿里云提供的短信服务发送短信
//            SMSUtils.sendMessage("","SMS_272580862",phone,code);
            //需要将生成的验证码保存到session中
            HttpSession session = request.getSession();
            session.setAttribute(phone,code);

            return R.success("短信发送成功");
        }
        return R.success("短信发送失败");
    }

    /**
     * 登录
     * @param map
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpSession session){
        //获取手机号
        String phone = map.get("phone").toString();
        //获取验证码
        String code = map.get("code").toString();
        Object attribute = session.getAttribute(phone);
        //进行验证码比对（前端传入的验证码与session中保存的验证码进行比对）
        if (attribute != null && attribute.equals(code)){
            //若比对成功，进入if逻辑，判断该手机号对应的用户是否为新用户
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getPhone,phone);
            User user = userService.getOne(queryWrapper);
            if (user == null){
                //若为新用户，完成注册
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            session.setAttribute("userId",user.getId());
            return R.success(user);
        }
        return R.error("验证码输入有误");
    }

    /**
     * 用户退出
     * @param session
     * @return
     */
    @PostMapping("/loginout")
    public R<String> loginOut(HttpSession session){
        //移除session中的userId，跳转页面
        session.removeAttribute("userId");
        return R.success("退出成功...");
    }
}
